<?php

use yii\db\Migration;

/**
 * Class m221109_133514_rbac_project
 */
class m221109_133514_rbac_project extends Migration
{
        /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $auth = Yii::$app->authManager;

        $permissionCreate = $auth->createPermission('createProject');
        $auth->add($permissionCreate);

        $permissionUpdate = $auth->createPermission('updateProject');
        $auth->add($permissionUpdate);

        $permissionDelete = $auth->createPermission('deleteProject');
        $auth->add($permissionDelete);

        $permissionBrowse = $auth->createPermission('browseProject');
        $auth->add($permissionBrowse);

        $userRole = $auth->createRole('projectUser');
        $auth->add($userRole);

        $managerRole = $auth->createRole('projectManager');
        $auth->add($managerRole);

        $auth->addChild($userRole, $permissionBrowse);

        $auth->addChild($managerRole, $permissionCreate);
        $auth->addChild($managerRole, $permissionUpdate);
        $auth->addChild($managerRole, $permissionDelete);
        $auth->addChild($managerRole, $permissionBrowse);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $auth = Yii::$app->authManager;

        $auth->remove($auth->getPermission('createProject'));
        $auth->remove($auth->getPermission('updateProject'));
        $auth->remove($auth->getPermission('deleteProject'));
        $auth->remove($auth->getPermission('browseProject'));
        $auth->remove($auth->getRole('projectUser'));
        $auth->remove($auth->getRole('projectManager'));
    }
}
